<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单管理 - 暖心助农选品平台</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.2/css/all.min.css" rel="stylesheet">
    <style>
        .sidebar {
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            z-index: 100;
            padding: 48px 0 0;
            box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
            background-color: #f8f9fa;
        }
        
        .sidebar-sticky {
            position: relative;
            top: 0;
            height: calc(100vh - 48px);
            padding-top: .5rem;
            overflow-x: hidden;
            overflow-y: auto;
        }
        
        .nav-link {
            color: #333;
            font-weight: 500;
            padding: 0.5rem 1rem;
        }
        
        .nav-link:hover {
            color: #2ecc71;
        }
        
        .nav-link.active {
            color: #2ecc71;
            background-color: #e9ecef;
        }
        
        .nav-link i {
            margin-right: 0.5rem;
            width: 20px;
            text-align: center;
        }
        
        main {
            padding-top: 48px;
        }
        
        .order-card {
            transition: transform 0.2s;
        }
        
        .order-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 0 15px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-success fixed-top">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">
                <i class="fas fa-leaf me-2"></i>暖心助农选品平台
            </a>
            <div class="d-flex align-items-center">
                <span class="text-white me-3">
                    <i class="fas fa-user-circle me-1"></i>
                    <span sec:authentication="name"></span>
                </span>
                <form th:action="@{/logout}" method="post" class="m-0">
                    <button type="submit" class="btn btn-outline-light btn-sm">
                        <i class="fas fa-sign-out-alt me-1"></i>退出
                    </button>
                </form>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
                <div class="sidebar-sticky">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="/admin/dashboard">
                                <i class="fas fa-tachometer-alt"></i>仪表板
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/admin/products">
                                <i class="fas fa-box"></i>商品管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/admin/users">
                                <i class="fas fa-users"></i>用户管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="/admin/orders">
                                <i class="fas fa-shopping-cart"></i>订单管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/admin/settings">
                                <i class="fas fa-cog"></i>系统设置
                            </a>
                        </li>
                    </ul>
                </div>
            </nav>

            <!-- 主要内容区域 -->
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">订单管理</h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <div class="btn-group me-2">
                            <button type="button" class="btn btn-sm btn-outline-success">
                                <i class="fas fa-download me-1"></i>导出订单
                            </button>
                        </div>
                    </div>
                </div>

                <!-- 筛选栏 -->
                <div class="card mb-4">
                    <div class="card-body">
                        <form th:action="@{/admin/orders}" method="get" class="row g-3">
                            <div class="col-md-3">
                                <input type="text" class="form-control" name="keyword" 
                                       th:value="${param.keyword}" placeholder="搜索订单号或商品名称">
                            </div>
                            <div class="col-md-2">
                                <select class="form-select" name="status">
                                    <option value="">全部状态</option>
                                    <option value="0" th:selected="${param.status == '0'}">待发货</option>
                                    <option value="1" th:selected="${param.status == '1'}">已发货</option>
                                    <option value="2" th:selected="${param.status == '2'}">已完成</option>
                                    <option value="3" th:selected="${param.status == '3'}">已取消</option>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <select class="form-select" name="timeRange">
                                    <option value="">全部时间</option>
                                    <option value="7" th:selected="${param.timeRange == '7'}">最近7天</option>
                                    <option value="30" th:selected="${param.timeRange == '30'}">最近30天</option>
                                    <option value="90" th:selected="${param.timeRange == '90'}">最近90天</option>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <button type="submit" class="btn btn-success w-100">
                                    <i class="fas fa-search me-1"></i>搜索
                                </button>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- 订单列表 -->
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>订单号</th>
                                        <th>下单时间</th>
                                        <th>用户</th>
                                        <th>总金额</th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:if="${#lists.isEmpty(orders.records)}">
                                        <td colspan="6" class="text-center">暂无订单</td>
                                    </tr>
                                    <tr th:each="order : ${orders.records}">
                                        <td th:text="${order.orderNo}">订单号</td>
                                        <td th:text="${#temporals.format(order.createTime, 'yyyy-MM-dd HH:mm')}">下单时间</td>
                                        <td th:text="${order.employeeId}">用户</td>
                                        <td th:text="${'¥' + #numbers.formatDecimal(order.totalAmount, 1, 2)}">总金额</td>
                                        <td>
                                            <span th:class="'badge ' + 
                                                ${order.status == 0 ? 'bg-warning' : 
                                                (order.status == 1 ? 'bg-info' : 
                                                (order.status == 2 ? 'bg-success' : 'bg-secondary'))}"
                                                th:text="${order.status == 0 ? '待发货' : 
                                                        (order.status == 1 ? '已发货' : 
                                                        (order.status == 2 ? '已完成' : '已取消'))}">
                                                状态
                                            </span>
                                        </td>
                                        <td>
                                            <button class="btn btn-sm btn-outline-primary me-1" 
                                                    th:onclick="'viewOrder(' + ${order.id} + ')'">
                                                <i class="fas fa-eye"></i>
                                            </button>
                                            <button th:if="${order.status == 0}" 
                                                    class="btn btn-sm btn-success me-1"
                                                    th:onclick="'shipOrder(' + ${order.id} + ')'">
                                                <i class="fas fa-truck"></i>
                                            </button>
                                            <button th:if="${order.status == 0}" 
                                                    class="btn btn-sm btn-danger"
                                                    th:onclick="'cancelOrder(' + ${order.id} + ')'">
                                                <i class="fas fa-times"></i>
                                            </button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>

                        <!-- 分页 -->
                        <nav th:if="${orders.pages > 1}" class="mt-4">
                            <ul class="pagination justify-content-center">
                                <li class="page-item" th:classappend="${orders.current == 1} ? 'disabled'">
                                    <a class="page-link" th:href="@{/admin/orders(page=${orders.current - 1}, size=${orders.size}, keyword=${param.keyword}, status=${param.status}, timeRange=${param.timeRange})}">
                                        上一页
                                    </a>
                                </li>
                                <li class="page-item" th:each="pageNum : ${#numbers.sequence(1, orders.pages)}"
                                    th:classappend="${pageNum == orders.current} ? 'active'">
                                    <a class="page-link" th:href="@{/admin/orders(page=${pageNum}, size=${orders.size}, keyword=${param.keyword}, status=${param.status}, timeRange=${param.timeRange})}"
                                       th:text="${pageNum}">1</a>
                                </li>
                                <li class="page-item" th:classappend="${orders.current == orders.pages} ? 'disabled'">
                                    <a class="page-link" th:href="@{/admin/orders(page=${orders.current + 1}, size=${orders.size}, keyword=${param.keyword}, status=${param.status}, timeRange=${param.timeRange})}">
                                        下一页
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <!-- Bootstrap JS 和 Popper.js -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/js/bootstrap.bundle.min.js"></script>
    <script>
        function viewOrder(orderId) {
            window.location.href = `/admin/orders/${orderId}`;
        }

        function shipOrder(orderId) {
            if (confirm('确定要发货吗？')) {
                fetch(`/admin/orders/${orderId}/ship`, {
                    method: 'POST'
                }).then(response => {
                    if (response.ok) {
                        location.reload();
                    }
                });
            }
        }

        function cancelOrder(orderId) {
            if (confirm('确定要取消这个订单吗？')) {
                fetch(`/admin/orders/${orderId}/cancel`, {
                    method: 'POST'
                }).then(response => {
                    if (response.ok) {
                        location.reload();
                    }
                });
            }
        }
    </script>
</body>
</html> 